package a66weding.com.jiehuntong.activity.order;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import org.xutils.x;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.me.MeBaseActivity;
import a66weding.com.jiehuntong.activity.order.adapter.RefundAdapter;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MyOrderBean;
import a66weding.com.jiehuntong.beans.ShipBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.CommonAdapter;
import a66weding.com.jiehuntong.common.MyViewHolder;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;

/**
 * Created by admin on 2016/9/1.
 * 物流信息;
 */
public class LogisticsActivity extends MeBaseActivity implements OnNetResultListener, View.OnClickListener {
    //左侧标题
    private LinearLayout mProgressLay;
    private ViewGroup mContentLay;
    private LinearLayout mNoNetLay;

    private LinearLayout mBackLay;
    private TextView mBackContentTv;
    private TextView mHeaderTitleTv;
    private TextView mRightTitleTv;

    private ListView mListview;

    //头像
    private ImageView logistics_icon;
    //物流状态;
    private TextView logistics_state_tv;
    //订单编号
    private TextView logistics_num_tv;
    //承运商家
    private TextView logistics_bussiness;
    private TextView mNoShipInfoTv;
    private int ordersID;
    private ShipBean.DataBean shipData;
    private List<ShipBean.DataBean.ListBean> shipList;

    private ShipAdapter mAdapter;

    private int shipStateM;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics);

        Intent intent = getIntent();

        if(intent!=null) {

            ordersID = intent.getIntExtra("ordersID", 1);

        }

        initView();

        initData();
    }
    private void initView(){

        mNoNetLay = (LinearLayout) findViewById(R.id.NoNetLay);
        mProgressLay = (LinearLayout) findViewById(R.id.progressLay);
        mContentLay = (ViewGroup) findViewById(R.id.lay_content);

        mBackLay = (LinearLayout) findViewById(R.id.llay_header_back);

        mBackContentTv = (TextView) findViewById(R.id.tv_header_backcontent);
        mHeaderTitleTv = (TextView) findViewById(R.id.tv_header_title);
        mRightTitleTv = (TextView) findViewById(R.id.tv_header_function);

        mListview = (ListView) findViewById(R.id.logistics_lv);
        logistics_bussiness = (TextView) findViewById(R.id.logistics_bussiness);
        logistics_state_tv = (TextView) findViewById(R.id.logistics_state_tv);


        logistics_icon = (ImageView) findViewById(R.id.logistics_icon);
        logistics_num_tv = (TextView) findViewById(R.id.logistics_num_tv);

        mNoShipInfoTv = (TextView) findViewById(R.id.tv_ship_no);
        mNoShipInfoTv.setVisibility(View.GONE);


        mBackContentTv.setText("我的订单");
        mHeaderTitleTv.setText("物流信息");
        mRightTitleTv.setVisibility(View.GONE);


        mBackLay.setOnClickListener(this);

    }

    private void initData(){

        getShipInfo();


    }

    private void setData() {

        if(shipData!=null) {
            //订单状态(物流状态)
            String status = shipData.getStatus();
            String shipState="";
            if("0".equals(status)) {
                shipState="待签收";
                shipStateM=0;
            }else if("1".equals(status)){
                shipState="已签收";
                shipStateM=1;
            }

            logistics_state_tv.setText(shipState);
            logistics_state_tv.setTextColor(getResources().getColor(R.color.colorMainRed));
            //订单编号
            logistics_num_tv.setText(shipData.getNo());
            //承运商家
            logistics_bussiness.setText(shipData.getCompany());

            shipList = shipData.getList();
            if(shipList!=null && shipList.size()>0) {
                setAdapter();
            }
        }
    }


    private void getShipInfo(){
        String url= RequestCode.get_KuaiDiByDanHao;
        url += "&ordersID="+ordersID;
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.GETREQUEST, url, this, ShipBean.class, RequestCode.get_KuaiDiByDanHao);
        HttpHandleUtil.executeCommonGet(this,httpRequestBean);

    }




    private void setAdapter(){

        mAdapter=new ShipAdapter();

        mListview.setAdapter(mAdapter);

    }




    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        refreshViewLay(HAVEDATA,mContentLay,mProgressLay,mNoNetLay);
        if(data instanceof ShipBean) {
            ShipBean sData= (ShipBean) data;
            switch (sData.getResCode()) {
                    case "0000":
                        shipData = sData.getData();
                        setData();
                        break;
                     case "0003":

                     default:
                         mNoShipInfoTv.setVisibility(View.VISIBLE);
            }
        }
    }



    @Override
    public void onError(ErrorBean errorBean) {
        switch (errorBean.getErrorCode()) {
                case ErrorBean.NONET:


                 case ErrorBean.TIMEOUT:
                     refreshViewLay(NONET,mContentLay,mProgressLay,mNoNetLay);
                break;
                 default:
                     CUtils.showDataErrToast(this);
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
                case R.id.llay_header_back:
                    finish();
                break;

        }
    }


    public class ShipAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return shipList.size();
        }

        @Override
        public Object getItem(int position) {
            return shipList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ShipBean.DataBean.ListBean shipListBean = shipList.get(position);


                convertView=View.inflate(LogisticsActivity.this,R.layout.item_refund,null);
            TextView item_refund_tv_content = (TextView) convertView.findViewById(R.id.item_refund_tv_content);
            TextView item_refund_tv_time = (TextView) convertView.findViewById(R.id.item_refund_tv_time);
               View item_refund_line1 =  convertView.findViewById(R.id.item_refund_line1);
            ImageView item_refund_icon = (ImageView) convertView.findViewById(R.id.item_refund_icon);
            View item_refund_line2 =  convertView.findViewById(R.id.item_refund_line2);

            item_refund_tv_content.setText(shipListBean.getRemark());
            item_refund_tv_time.setText(shipListBean.getDatetime());

            if (position==0){
               item_refund_line1.setVisibility(View.INVISIBLE);
               item_refund_icon.setImageResource(R.mipmap.reddian);
               item_refund_line2.setVisibility(View.VISIBLE);
            }else if(position==shipList.size()-1){

               item_refund_line1.setVisibility(View.VISIBLE);
                if(shipStateM==0) {
                    item_refund_icon.setImageResource(R.mipmap.huidian);
                }else if(shipStateM==1){
                    item_refund_icon.setImageResource(R.mipmap.reddian);
                }

                item_refund_line2.setVisibility(View.INVISIBLE);

            }else{
               item_refund_line1.setVisibility(View.VISIBLE);

               item_refund_line2.setVisibility(View.VISIBLE);
            }



            return convertView;
        }

        class ViewHolder{

            TextView item_refund_tv_content;
            TextView item_refund_tv_time;
            View item_refund_line1;
            ImageView item_refund_icon;
            View item_refund_line2;



        }

    }
}
